package com.yyh.photo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyh.photo.domain.CommodityDomain;
import com.yyh.photo.entity.CommodityEntity;
import com.yyh.photo.entity.CommodityFileEntity;
import com.yyh.photo.mapper.CommodityMapper;
import com.yyh.photo.service.CommodityFileService;
import com.yyh.photo.service.CommodityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * service 实现 类
 *
 * @author jxjk.safe
 * @date 2023-06-07 17:15:10
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, CommodityEntity> implements CommodityService {

    @Autowired
    CommodityFileService fileService;

    /**
     * 分页查询
     *
     * @param pageParam
     * @param commodityDomain
     * @return
     */
    @Override
    public List<CommodityDomain> pageQuery(Page<CommodityDomain> pageParam, CommodityDomain commodityDomain) {
        List<CommodityDomain> commodityDomainList = this.getBaseMapper().pageQuery(pageParam, commodityDomain);
        if (!CollectionUtils.isEmpty(commodityDomainList)) {
            for (CommodityDomain commodity : commodityDomainList) {
                QueryWrapper<CommodityFileEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(CommodityFileEntity::getCommodityId, commodity.getId());
                List<CommodityFileEntity> fileEntityList = fileService.list(queryWrapper);
                if (!CollectionUtils.isEmpty(fileEntityList)) {
                    commodity.setFileList(fileEntityList);
                } else {
                    commodity.setFileList(new ArrayList<>());
                }
            }
        }
        return commodityDomainList;
    }

    @Override
    public List<CommodityDomain> getlist(CommodityDomain commodityDomain) {
        return this.getBaseMapper().getList(commodityDomain);
    }

    @Override
    @Transactional
    public boolean saveCommodity(CommodityDomain commodityEntity) {
        CommodityEntity commodity = new CommodityEntity();
        BeanUtils.copyProperties(commodityEntity, commodity);
        int flag = this.getBaseMapper().insert(commodity);
        if (!CollectionUtils.isEmpty(commodityEntity.getFileList())) {
            for (CommodityFileEntity file : commodityEntity.getFileList()) {
                file.setCommodityId(commodity.getId());
                fileService.save(file);
            }
        }
        return flag > 0;
    }

    @Override
    public CommodityDomain getDetailById(Integer id) {
        CommodityEntity commodity = this.getById(id);
        CommodityDomain commodityDomain = new CommodityDomain();
        if (commodity != null) {
            BeanUtils.copyProperties(commodity, commodityDomain);
            QueryWrapper<CommodityFileEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(CommodityFileEntity::getCommodityId, id);
            List<CommodityFileEntity> fileEntityList = fileService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(fileEntityList)) {
                commodityDomain.setFileList(fileEntityList);
            } else {
                commodityDomain.setFileList(new ArrayList<>());
            }
        }
        return commodityDomain;
    }

}